
$(function () {

    $("#top").load('top.html');
    $("#left").load('department_left.html',function () {
        menuActive(1,0)
    });

    //初始化学院下拉列表
    requests.getReq(urls.getCollege,'',function (data) {
        if(data.status == 1){
            var collegeOpts = '';
            var list = data.data;
            for(var key in list){
                collegeOpts += '<option value="'+key+'">'+list[key]+'</option>>';
            }
            $("#collegeSel").append(collegeOpts);

        }else{
            showTip('服务器异常');
        }
    },function () {
        showTip('服务器异常');
    })

    //初始化学历下拉列表
    var degreeOpts = '';
    for(var key in commonEducation){
        degreeOpts += '<option value="'+key+'">'+commonEducation[key]+'</option>>';
    }
    $("#degreeSel").append(degreeOpts);

    //初始化职称下拉列表
    var professOpts = '';
    for(var key in commonTitle){
        professOpts += '<option value="'+key+'">'+commonTitle[key]+'</option>>';
    }
    $("#professTideSel").append(professOpts);


    /*--------------绑定事件--------------------------------------------------------*/
    //选择学院后，获取学院对应的教研室
    $("#collegeSel").change(function () {
        var selCollege = {
            collegeId: $(this).val()
        }
        requests.getReq(urls.getResearch,selCollege,function (data) {
            if(data.status == 1){
                var researOpts = '<option value="">-- 请选择 --</option>>';
                var list = data.data;
                $.each(list,function (i,val) {
                    researOpts += '<option value="'+val.researchId+'">'+val.researchName+'</option>>';
                })
                $("#tea_research").html(researOpts);

            }else{
                showTip('服务器异常');
            }
        },function () {
            showTip('服务器异常');
        })
    })

    //点击添加教师按钮
    $("#addSubmit").click(function () {
        var hiringTimeArr = $("#datepicker").val();
        var hiringTime = new Date(hiringTimeArr);

        var addParam = {
            "collegeId": $("#collegeSel").val(),//所属学院
            "education": $("#degreeSel").val(),//学历
            "title": $("#professTideSel").val(),//职称
            'teacherPostLevel': $("#tea_stationLevel").val(),//岗位级别
            "sex": $("#tea_gender").val(),//性别
            "hiringTime": hiringTime,//聘用时间
            "researchId": $("#tea_research").val(),//所属教研室
            "userId": $("#tea_userId").val(),//教师编号
            "userName": $("#tea_userName").val()//教师姓名
        }
        var compFlag = true;
        for(var key in addParam){
            if(addParam[key] == '' || addParam[key] == ' '){
                showTip('请将教师信息补充完整再进行提交');
                compFlag = false;
                break;
            }
        }
        if(compFlag){
            requests.postReq(urls.addTeacher,JSON.stringify(addParam),function(data){
                if(data.status == 1){
                    showTip('教师 '+addParam.userName+' 添加成功',function(){
                        window.location.reload();
                    });
                }else{
                    showTip(data.message);
                }
            },function(data){
                console.log('服务器错误');
            })
        }
    })

})

